package code.google.model.groove.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class CryptHelper {
	
	private static String secret = GrooveConstants.SECRET_KEY; 
	
	public static String GetMD5Hash(String paramString) {
		try {
			MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
			localMessageDigest.update(paramString.getBytes());
			return String.format("%1$032x", new Object[] { new BigInteger(1,
					localMessageDigest.digest()) });
		} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
		}
		return null;
	}

	public static String GetSHA1Hash(String paramString) {
		try {
			MessageDigest localMessageDigest = MessageDigest.getInstance("SHA");
			localMessageDigest.update(paramString.getBytes());
			return String.format("%1$040x", new Object[] { new BigInteger(1,
					localMessageDigest.digest()) });
		} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
		}
		return null;
	}

	public static String getToken(String myCommunicationToken, String method) {
		String rand = GetRandomHexStr();
		
		return rand + GetSHA1Hash(method + ":" + myCommunicationToken + ":" + secret + ":" + rand);
	}
	
	 private  static String GetRandomHexStr()
	    {
	        char ac[] = new char[6];
	        Random random = new Random();
	        int i = 0;
	        do
	            ac[i] = "0123456789abcdef".toCharArray()[random.nextInt(16)];
	        while(++i <= 5);
	        return new String(ac);
	    }
}
